About 2051 letters

About 10 minutes

#Python's input & type conversion

#Standard input

We have already learned how to use the print function to display output. So, how do we get user input?

Calling the input function can get input from the command line, and the return value is of type str:

text:str = input("Please input:") print("What you input is", text)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Normally, the program is run using the command line. The above code will print "Please enter" as an input prompt, and then wait for user input.
After the user enters the content, press Enter to confirm, the program obtains the input content, and then continues to run.

The online operating environment Shift used by this site does not support user interaction due to WASM security restrictions.
The input method is to pre-enter the content in the input box at the bottom, and then click the RUN button to execute the program.
When the program runs, the content of the input box will be automatically provided to input.

#Type conversion

If we need to input an integer, we need to convert the value to a different type. The syntax for type conversion is type(value):

G:float = 9.8 # Gravity acceleration text:str = input("Please enter the mass of the object (kg):") mass:float = float(text) print("The gravity on the object is", G * mass)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Practise

Please implement the calculation of the surface area and volume of the sphere, and obtain the radius through input.

  • The formula for the surface area of ​​a sphere is
  • The formula for the volume of a sphere is
PI:float = 3.1415926 radius:float = 0 # Modify the code here to get the radius through input area:float = 0 # Modify the code here to calculate the surface area volume:float = 0 # Modify the code here to calculate the volume print("The surface area and volume of ​​a circle with a radius of", radius, "are", area, "and", volume)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Created in 5/15/2025

Updated in 5/21/2025